$(function () {
    inputRadioSel($(".i-checks"), selectCall);//查询所有单选按钮，回调方法selectCall
    $('body').on('click', '.selectData', function () {
        var dataType=$(this).data('type');
        _selectData(dataType);
    });
    $('body').on('click', '.deleteData', function () {
        var obj = $(this).parents('tr');
        obj.remove();
        resetSortIcon();
    });
    $('body').on('click', '#onBack', function () {
        onBackHistory();
    }).on('click', 'i.fa-arrow-up', function () { // 上移
        var _this = $(this).parents('tr');
        var prev = _this.prev('tr');
        if (prev && prev.is(':visible')) {
            //交换排序标识
            var BeforeSort = _this.data('sort');
            var prevSort = prev.data('sort');
            _this.data('sort',prevSort);
            prev.data('sort',BeforeSort);
            _this.insertBefore(prev);
        }
        resetSortIcon();
    }).on('click', 'i.fa-arrow-down', function () { // 下移
        var _this = $(this).parents('tr');
        var next = _this.next('tr');
        if (next && next.is(':visible')) {
            //交换排序标识
            var aftereSort = _this.data('sort');
            var nextSort = next.data('sort');
            _this.data('sort',nextSort);
            next.data('sort',aftereSort);
            _this.insertAfter(next);
        }
        resetSortIcon();
    });
    submitGroomData();
});

/**
 * 取消
 */
function onBackHistory() {
    window.history.go(-1);
}

function _selectData(dataType) {
    if(dataType=='COURSE'){
        selectCourse({
            selectType:"checkbox",
            type:1,
            callback:'selectCourseCallback'
        });
    }
    if(dataType=='USER'){
        selectUser("checkbox","");
    }
    if(dataType=='ARTICLE'){
        selectArticle("checkbox",'');
    }
    if(dataType=='TEACHER'){
        selectTeacher("checkbox",'','selectTeacherCallback','1');
    }
    if(dataType=='BOOK'){
        selectBook({
            selectType:'checkbox',
            callback:'selectBookCallback'
        });
    }
    if(dataType=='PACKAGE'){
        selectPackage("checkbox",'');
    }
    if(dataType =='LIVE'){
        selectCourse({
            selectType:"checkbox",
            type:3,
            callback:'selectCourseCallback'
        });
    }
    if(dataType =='SMALL'){
        selectCourse({
            selectType:"checkbox",
            type:4,
            callback:'selectCourseCallback'
        });
    }
}

/**
 * 用户回调
 * @param result
 */
function selectUserCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
            var trs = $('#data-content').find('tr');
            $.each(trs,function (index,tr) {
                var td = $(tr).find('td')[0];
                dataIds += $(td).text()+',';
            });
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.userName;
          var dataTo = dataIds.split(",");
            if (dataTo.indexOf(id) == -1) {
                dataIds += id + ","
                var length =  $('#data-content').find('tr').length+1;
                $('#data-content').append(' <tr data-id="'+id+'" data-sort="'+length+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);"><i class="fa fa-trash"></i></a>'
                    +'<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="上移">' +
                    '<i class="fa fa-arrow-up"></i></a>' +
                    '<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="下移">' +
                    '<i class="fa fa-arrow-down"></i></a>'+
                    '</td>'+
                    '</tr>'
                );
            }
        });
        resetSortIcon();
        $('input[name="dataIds"]').val(dataIds);
        $("#groomData").removeClass('hide');
    }
}

/**
 * 课程回调
 * @param result
 */
function selectCourseCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
            var trs = $('#data-content').find('tr');
            $.each(trs,function (index,tr) {
                var td = $(tr).find('td')[0];
                dataIds += $(td).text()+',';
            });
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.courseName;

            var dataTo = dataIds.split(",");
            if (dataTo.indexOf(id) == -1){
                dataIds += id + ",";
                //获取页面上的数据数量添加排序标识
               var length =  $('#data-content').find('tr').length+1;
                $('#data-content').append(' <tr data-id="'+id+'" data-sort="'+length+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);"><i class="fa fa-trash"></i></a>'
                    +'<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="上移">' +
                    '<i class="fa fa-arrow-up"></i></a>' +
                    '<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="下移">' +
                    '<i class="fa fa-arrow-down"></i></a>'+
                    '</td>'+
                    '</tr>'
            );
            }
        });
        resetSortIcon();
        $('[data-toggle=tooltip]').tooltip();
        $('input[name="dataIds"]').val(dataIds);
        $("#groomData").removeClass('hide');
    }
}

/**
 * 重置试题题型排序图标
 */
function resetSortIcon() {
    $('#data-content tr[data-sort]').each(function () {
        $(this).find('i.fa-arrow-up, i.fa-arrow-down').parents('a').removeClass('hide');
    });
    $('#data-content tr[data-sort]:first').find('i.fa-arrow-up').parents('a').addClass('hide');
    $('#data-content tr[data-sort]:last').find('i.fa-arrow-down').parents('a').addClass('hide');
}
/**
 * 讲师回调
 * @param result
 */
function selectTeacherCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
            var trs = $('#data-content').find('tr');
            $.each(trs,function (index,tr) {
                var td = $(tr).find('td')[0];
                dataIds += $(td).text()+',';
            });
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.teacherName;
            var dataTo = dataIds.split(",");
            if (dataTo.indexOf(id) == -1) {
                dataIds += id + ","
                var length =  $('#data-content').find('tr').length+1;
                $('#data-content').append(' <tr data-id="'+id+'" data-sort="'+length+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);"><i class="fa fa-trash"></i></a>'
                    +'<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="上移">' +
                    '<i class="fa fa-arrow-up"></i></a>' +
                    '<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="下移">' +
                    '<i class="fa fa-arrow-down"></i></a>'+
                    '</td>'+
                    '</tr>'
                );
            }
        });
        resetSortIcon();
        $('input[name="dataIds"]').val(dataIds);
        $("#groomData").removeClass('hide');
    }
}

/**
 * 图书回调
 * @param result
 */
function selectBookCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
            var trs = $('#data-content').find('tr');
            $.each(trs,function (index,tr) {
                var td = $(tr).find('td')[0];
                dataIds += $(td).text()+',';
            });
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.bookName;
            if (dataIds.indexOf("," + id+ ",") == -1 && dataIds.indexOf(id + ",") == -1) {
                dataIds += id + ","
                var length =  $('#data-content').find('tr').length+1;
                $('#data-content').append(' <tr data-id="'+id+'" data-sort="'+length+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);"><i class="fa fa-trash"></i></a>'
                    +'<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="上移">' +
                    '<i class="fa fa-arrow-up"></i></a>' +
                    '<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="下移">' +
                    '<i class="fa fa-arrow-down"></i></a>'+
                    '</td>'+
                    '</tr>'
                );
            }
        });
        resetSortIcon();
        $('input[name="dataIds"]').val(dataIds);
        $("#groomData").removeClass('hide');
    }
}

/**
 * 套餐回调
 * @param result
 */
function selectPackageCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
            var trs = $('#data-content').find('tr');
            $.each(trs,function (index,tr) {
                var td = $(tr).find('td')[0];
                dataIds += $(td).text()+',';
            });
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.courseName;
            if (dataIds.indexOf("," + id+ ",") == -1 && dataIds.indexOf(id + ",") == -1) {
                dataIds += id + ","
                var length =  $('#data-content').find('tr').length+1;
                $('#data-content').append(' <tr data-id="'+id+'" data-sort="'+length+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);"><i class="fa fa-trash"></i></a>'
                    +'<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="上移">' +
                    '<i class="fa fa-arrow-up"></i></a>' +
                    '<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="下移">' +
                    '<i class="fa fa-arrow-down"></i></a>'+
                    '</td>'+
                    '</tr>'
                );
            }
        });
        resetSortIcon();
        $('input[name="dataIds"]').val(dataIds);
        $("#groomData").removeClass('hide');
    }
}


/**
 * 套餐回调
 * @param result
 */
function selectArticleCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
            var trs = $('#data-content').find('tr');
            $.each(trs,function (index,tr) {
                var td = $(tr).find('td')[0];
                dataIds += $(td).text()+',';
            });
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.articleName;
            if (dataIds.indexOf("," + id+ ",") == -1 && dataIds.indexOf(id + ",") == -1) {
                dataIds += id + ","
                var length =  $('#data-content').find('tr').length+1;
                $('#data-content').append(' <tr data-id="'+id+'" data-sort="'+length+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);"><i class="fa fa-trash"></i></a>'
                    +'<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="上移">' +
                    '<i class="fa fa-arrow-up"></i></a>' +
                    '<a href="javascript:void(0)" class="c-master fs14 ml5 hide" data-toggle="tooltip" data-placement="top" data-original-title="下移">' +
                    '<i class="fa fa-arrow-down"></i></a>'+
                    '</td>'+
                    '</tr>'
                );
            }
        });
        resetSortIcon();
        $('input[name="dataIds"]').val(dataIds);
        $("#groomData").removeClass('hide');
    }
}


function submitGroomData() {
    $('body').on('click', "#submitGroomData", function () {
        var dataType = $('input[name="dataType"]').val();
        if (dataType == null || dataType == '') {
            dataType = $('input[name="groomPlaceDataType"]:checked').val();
        }
        var placeKey = $('input[name="placeKey"]').val();
        var dataIds = '';
        var dataSorts = '';
        $("#data-content tr").each(function () {
            dataIds += $(this).data('id') + ",";
            dataSorts+=$(this).data('sort') + ",";
        });
        var groomNum = $('input[name="groomNum"]').val();
        $.ajax({
            url: baseloaction + "/admin/op/groom/saveUpdateOpGroomData",
            type: "post",
            data: {"dataType": dataType, "placeKey": placeKey, "dataIds": dataIds, "groomNum": groomNum,"dataSorts":dataSorts},
            dataType: "json",
            success: function (result) {
                if (result.success == true) {
                    icModale(1, result.message, 1, function () {
                        window.location.href = baseloaction + "/admin/op/groom/queryGroomPlaceList"
                    });
                } else {
                    icModale(0, result.message);
                }
            },
            error: function (error) {
                icModale(0, '系统错误，数据加载失败');
            }
        });
    })
}

/**
 * 选择回调
 */
function selectCall(em) {
    if (em.name == 'groomPlaceDataType') {
        var typeName = '';
        var dataType = '';
        if (em.value == 'COURSE') {
            typeName = '课程';
            dataType = 'COURSE';
        }
        if (em.value == 'USER') {
            typeName = '学员';
            dataType = 'USER';
        }
        if (em.value == 'TEACHER') {
            typeName = '讲师';
            dataType = 'TEACHER';
        }
        if (em.value == 'ARTICLE') {
            typeName = '新闻资讯';
            dataType = 'ARTICLE';
        }
        if (em.value == 'BOOK') {
            typeName = '图书';
            dataType = 'BOOK';
        }
        if (em.value == 'PACKAGE') {
            typeName = '套餐';
            dataType = 'PACKAGE';
        }
        $('#recommendedData').html('推荐' + typeName);
        $('#selectData').html('<i class="fa fa-plus"></i> &nbsp;选择' + typeName);
        $('input[name="groomPlace.dataType"]').val(dataType);
        _selectData();
    }
}